package com.huluobo.hyj.hyjmodel.ui.activity

import com.huluobo.hyj.hyjmodel.R
import com.huluobo.hyj.hyjmodel.contract.register.RegisterContract
import com.huluobo.hyj.hyjmodel.contract.register.RegisterPresenter
import kotlinx.android.synthetic.main.activity_register.*
import org.jetbrains.anko.toast

class RegisterActivity : BaseActivity() ,RegisterContract.View{


    //实例化 等于这个页面
    private val Presenter = RegisterPresenter(this)

    override fun init() {
        super.init()
        register.setOnClickListener {
            register()
        }
        confirmPassword.setOnEditorActionListener { v, actionId, event ->
            register()
            true
        }
    }
    private fun register(){
        //隐藏软键盘
        hideSoftKeyboard()
        val userNameString = userName.text.trim().toString()
        val passwordString = password.text.trim().toString()
        val confirmString = confirmPassword.text.trim().toString()
        //注册
        Presenter.register(userNameString,passwordString,confirmString)
    }

    //加载布局
    override fun getLayoutResId(): Int = R.layout.activity_register
    //用户名
    override fun onUserNameError() {
        userName.error = getString(R.string.user_name_error)
    }

    override fun onPasswordError() {
        password.error = getString(R.string.password_error)
    }

    override fun onConfirmPasswordError() {
        confirmPassword.error = getString(R.string.confirm_password_error)
    }

    override fun onStartregister() {
        showProgress(getString(R.string.registering))//正在注册
    }

    override fun onRegisterSuccess() {  //注册成功
        dismissProgress()
        finish()
    }

    override fun onRegisterFailed(message: String) {
        dismissProgress()
        toast(message + "," + R.string.register_failed) //注册失败
    }
    override fun onUserExist() {
        dismissProgress()
        toast(R.string.user_already_exist) //用户重复
    }


}